//
//  MonthBottomView.m
//  carcareIOS
//
//  Created by ileo on 16/7/8.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "MonthBottomView.h"

@interface MonthBottomView ()

@property (nonatomic, strong) UIButton *flexButton;
@property (nonatomic, strong) CAShapeLayer *flexLayer;

@end

@implementation MonthBottomView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.clipsToBounds = YES;
        UIView *circle = [[[[UIView viewWithFrame:CGRectMake(0, 0, 36, 36)] resetCornerRadius:18] resetBackgroundColor:[UIColor whiteColor]] setupOnView:self];
        self.backgroundColor = [UIColor clearColor];
        [self.layer addSublayer:self.flexLayer];
        [self addSubview:self.flexButton];
        circle.center = CGPointMake(self.flexButton.centerX, 0);
        self.flexLayer.position = CGPointMake(self.flexButton.centerX, self.flexButton.centerY - 5);
    }
    return self;
}

-(void)updateFlexState:(FLEX_STATE)state{
    self.flexLayer.transform = CATransform3DRotate(CATransform3DIdentity, ((state == FLEX_OPEN ? 180 : 0)*M_PI/180), 0, 0, 1);
}

#pragma mark - set get

-(UIButton *)flexButton{
    if (!_flexButton) {
        __weak __typeof(self) wself = self;
        _flexButton = [UIButton buttonEmptyWithFrame:CGRectMake(self.width - 60, 0, 60, self.height) click:^{
            if (wself.delegate) {
                [wself.delegate monthBottomViewClickOnFlex];
            }
        }];
    }
    return _flexButton;
}

-(CAShapeLayer *)flexLayer{
    if (!_flexLayer) {
        _flexLayer = [CAShapeLayer layer];
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathMoveToPoint(path, NULL, -8, -4);
        CGPathAddLineToPoint(path, NULL, 0, 4);
        CGPathAddLineToPoint(path, NULL, 8, -4);
        _flexLayer.strokeColor = COLOR_HEXSTRING(@"#a4a4a4").CGColor;
        _flexLayer.fillColor = [UIColor clearColor].CGColor;
        _flexLayer.lineJoin = kCALineJoinRound;
        _flexLayer.lineCap = kCALineCapRound;
        _flexLayer.lineWidth = 2;
        _flexLayer.path = path;
        CGPathRelease(path);
    }
    return _flexLayer;
}

@end
